import re
import xlwt
from openpyxl import Workbook
from numpy import *
import pymysql
import time, datetime

# 获取时间
from heiben_py.main.util_db import DataBaseHandle

ticks = time.time()
# 加载数据库
DbHandle = DataBaseHandle()
# 读取参数文件，将文件根据每行的空格隔开

h_id_s = DbHandle.selectDb_2(
    "SELECT id from machine_hardware_information where hardware_ip='10.88.66.36' limit 1 "
)
id = h_id_s[0]['id']
t_id_s = DbHandle.selectDb_2(
    'select task_id from task where h_id = 14  and task_id =41')
task_id = t_id_s[0]['task_id']
data = []
sql = "INSERT INTO iostat (h_id,t_id,date,Device,tps,KB_readsped,KB_wrtnspeed,KB_read,KB_wrtn,AMorPM)VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"
with open('D://服务器跑下来的数据//20210122 64G测试结果//io_usage.log',
          'r',
          encoding='utf8') as f:
    cont = True
    li = []
    while cont:
        cont = f.readline()
        li.append(cont)
        if cont == '\n':
            if ('AM' in li[0] or 'PM' in li[0]):
                for i in range(2, len(li)):
                    item = li[i].split()
                    if len(item) == 6:
                        data.append((id, task_id, li[0].split()[1], item[0],
                                     item[1], item[2], item[3], item[4],
                                     item[5].replace('\n', ''),li[0].split()[2]))
            li = []
if len(data) > 0:
    DbHandle.insertDBmany(sql, data)
    DbHandle.closeDb()
f.close()